import { Component, OnInit } from '@angular/core';
import { ApesMessageService } from '@shared/apes/message';
import { ApiService } from '@core/services/http/api.service';

@Component({
  selector: 'administration',
  templateUrl: './administration.component.html',
  styleUrls: ['./administration.component.scss'],
})
export class AdministrationComponent implements OnInit {
  /* 按钮相关 */
  buttonBar = [
    { hide: true, style: 'default', label: '查询', id: 'action_query', loading: false },
  ];
  title = '';
  ctx = { model: {} };

  branch: any[];
  branchID: any[] = [];

  // 性别占比
  sexPieData: any;
  man: number;        // 男
  woman: number;      // 女
  // 年龄占比
  yearPieData: any;
  youth: number;      // 16-40
  betire: number;     // 退休
  wrinkly: number;    // 40-退休前
  // 学历占比
  educationData: any[];
  senior: number;     // 高中
  junior: number;     // 大专
  master: number;     // 硕士
  undergraduate: number;   // 本科
  below: number;      // 高中以下
  other: number;      // 其他
  technical: number;  // 中技
  technology: number; // 高技
  university: number; // 电大
  special: number;    // 中专

  // 人员类型
  postGroupData: any[];
  driver: number;     // 司机/门卫组
  cosmetology: number;   // 美容组
  assistant: number;     // 助理组
  spray: number;         // 钣喷组
  shopowner: number;     // 店长组
  electromechanical: number;   // 机电组
  director: number;      // 总部主管及以上（含）
  warden: number;        // 助理区长组
  rest: number;          // 其他

  // 在职状态
  jobClassData: any[];
  partTime: number;    // 兼职
  job: number;         // 全职

  /**关联跳转**/
  relevancyMetadata;

  constructor(private apiService: ApiService,
              private msg: ApesMessageService) {
  }

  ngOnInit() {
    this.getBranch();
    this.queryData();
    this.relevancyMetadata = {
      links: [
        // { link: { title: '查询人员信息（绩效）', id: 'report/hr.achievements.query' } },
        // { link: { title: '查询人员信息（薪酬）', id: 'report/hr.emolument.query' } },
        // { link: { title: '查询人员信息（行政）', id: 'report/hr.administration.query' } },
        // { link: { title: '查询人员信息（入职）', id: 'report/hr.applyJob.query' } },
        // { link: { title: '查询人员信息（招聘）', id: 'report/hr.recruit.query' } },
        // { link: { title: '查询人员信息（合同）', id: 'report/hr.employeeContract.query' } },
        // { link: { title: '查询需签合同人员', id: 'report/hr.contract2.query' } },
        // { link: { title: '查询人员信息（门店绩效）', id: 'report/hr.achievementsStore.query' } },
        // { link: { title: '查询部门工作人员', id: 'report/hr.departmentWorker.query' } },
        { link: { title: '查询门店编制', id: 'report/hr.sponsorSupport.query' } },
        { link: { title: '查询人员构成', id: 'bi/administration/administrationComponent' } },
        { link: { title: '查询招聘时效', id: 'report/hr.efficiency.query' } },
        { link: { title: '同店同职两年', id: 'report/hr.transferTwoYear.query' } },
        { link: { title: '长期未晋升', id: 'report/hr.noPromotion.query' } },
        { link: { title: '平均工龄', id: 'report/hr.seniority.query' } },
      ],
    };
  }

  onEvent(data: any) {
    if (data instanceof MouseEvent) return;
    if (data) {
      if (data.id === 'action_query') {
        this.buttonBar[0].loading = true;
        this.queryData();
      }
    }
  }

  // 查询按钮事件
  queryData() {
    this.getTablesData();
    this.getYear();
    this.getEducation();
    this.getPostGroup();
    this.getJobClass();
  }

  // 性别
  getTablesData() {
    let data = { 'BRANCHID': this.branchID };
    this.apiService.post('hr.querySex', { 'body': JSON.stringify(data) }).subscribe(
      (body) => {
        if (body) {
          this.man = body['男'] ? body['男'] : 0;
          this.woman = body['女'] ? body['女'] : 0;
          // 加载 饼图
          this.sexPieData = this.getSexPie();
          this.buttonBar[0].loading = false;

        } else {
          this.msg.info(body.msg);
        }
      });
  }

  // 年龄
  getYear() {
    let data = { 'BRANCHID': this.branchID };
    this.apiService.post('hr.queryYear', { 'body': JSON.stringify(data) }).subscribe(
      (body) => {
        if (body) {
          this.youth = body[0].YOUTH ? body[0].YOUTH : 0;
          this.wrinkly = body[0].WRINKLY ? body[0].WRINKLY : 0;
          this.betire = body[0].RETIRE ? body[0].RETIRE : 0;

          // 加载 饼图
          this.yearPieData = this.getYearPie();
          this.buttonBar[0].loading = false;

        } else {
          this.msg.info(body.msg);
        }
      });
  }

  // 学历
  getEducation() {
    let data = { 'BRANCHID': this.branchID };
    this.apiService.post('hr.queryEducation', { 'body': JSON.stringify(data) }).subscribe(
      (body) => {
        if (body) {
          this.senior = body['高中'] ? body['高中'] : 0;
          this.junior = body['大专'] ? body['大专'] : 0;
          this.master = body['硕士'] ? body['硕士'] : 0;
          this.undergraduate = body['本科'] ? body['本科'] : 0;
          this.below = body['高中以下'] ? body['高中以下'] : 0;
          this.other = body['其他'] ? body['其他'] : 0;
          this.technical = body['中技'] ? body['中技'] : 0;
          this.technology = body['高技'] ? body['高技'] : 0;
          this.university = body['电大'] ? body['电大'] : 0;
          this.special = body['中专'] ? body['中专'] : 0;

          // 加载 饼图
          this.educationData = this.getEducationPie();
          this.buttonBar[0].loading = false;

        } else {
          this.msg.info(body.msg);
        }
      });
  }

  // 人员类型
  getPostGroup() {
    let data = { 'BRANCHID': this.branchID };
    this.apiService.post('hr.queryPostGroup', { 'body': JSON.stringify(data) }).subscribe(
      (body) => {
        if (body) {
          this.driver = body['司机/门卫组'] ? body['司机/门卫组'] : 0;
          this.cosmetology = body['美容组'] ? body['美容组'] : 0;
          this.assistant = body['助理组'] ? body['助理组'] : 0;
          this.spray = body['钣喷组'] ? body['钣喷组'] : 0;
          this.shopowner = body['店长组'] ? body['店长组'] : 0;
          this.electromechanical = body['机电组'] ? body['机电组'] : 0;
          this.director = body['总部主管及以上（含）'] ? body['总部主管及以上（含）'] : 0;
          this.warden = body['助理区长组'] ? body['助理区长组'] : 0;
          this.rest = body['其他'] ? body['其他'] : 0;

          // 加载 饼图
          this.postGroupData = this.getPostGroupPie();
          this.buttonBar[0].loading = false;

        } else {
          this.msg.info(body.msg);
        }
      });
  }

  // 在职状态
  getJobClass() {
    let data = { 'BRANCHID': this.branchID };
    this.apiService.post('hr.queryJobClass', { 'body': JSON.stringify(data) }).subscribe(
      (body) => {
        if (body) {
          this.partTime = body[0].PARTTIME ? body[0].PARTTIME : 0;
          this.job = body[0].JOB ? body[0].JOB : 0;

          // 加载 饼图
          this.jobClassData = this.getJobClassPie();
          this.buttonBar[0].loading = false;

        } else {
          this.msg.info(body.msg);
        }
      });
  }

  // 饼图
  getSexPie() {
    return [
      { x: '男', y: this.man },
      { x: '女', y: this.woman },
    ];
  }

  getYearPie() {
    return [
      { x: '16-40', y: this.youth },
      { x: '40-退休前', y: this.wrinkly },
      { x: '退休', y: this.betire },
    ];
  }

  getEducationPie() {
    return [
      { x: '高中', y: this.senior },
      { x: '大专', y: this.junior },
      { x: '硕士', y: this.master },
      { x: '本科', y: this.undergraduate },
      { x: '高中以下', y: this.below },
      { x: '中技', y: this.technical },
      { x: '高技', y: this.technology },
      { x: '电大', y: this.university },
      { x: '中专', y: this.special },
      { x: '其他', y: this.other },
    ];
  }

  getPostGroupPie() {
    return [
      { x: '司机/门卫组', y: this.driver },
      { x: '美容组', y: this.cosmetology },
      { x: '助理组', y: this.assistant },
      { x: '钣喷组', y: this.spray },
      { x: '店长组', y: this.shopowner },
      { x: '机电组', y: this.electromechanical },
      { x: '总部管理', y: this.director },
      { x: '助理区长组', y: this.warden },
      { x: '其他', y: this.rest },
    ];
  }

  getJobClassPie() {
    return [
      { x: '兼职', y: this.partTime },
      { x: '全职', y: this.job },
    ];
  }


  // 获取部门信息
  getBranch() {
    let data = { 'size': 10000, page: 0 };
    this.apiService.post('cm.branch.findAll', { 'body': JSON.stringify(data) }).subscribe(
      (body) => {
        if (body) {
          this.branch = body;
        } else {
          this.msg.info(body.msg);
        }
      });
  }

}
